﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SetUp : MonoBehaviour {
    public Button m_BICO;
    public GameObject m_back;
    public Button m_BYES;
    public Button m_BNO;
    bool IsFirst = true;
    bool m_bAlter = false;
    Toggle[] m_toggle;
    int count;
    public Scrollbar Scrollbar;
    private void Awake()
    {
        PlayerPrefs.DeleteKey("IsShowPianoInfo");
        m_back.SetActive(false);
        m_BICO.onClick.AddListener(delegate () { this.ShowAlter(); });
    }
    private void Update()
    {
        if (m_bAlter)
        {
            GlobalHelper.backGround.volume= Scrollbar.value;
        }
    }
    void ShowAlter()
    {
        m_back.SetActive(true);
        m_BICO.interactable = false;
        m_bAlter = true;
        if (IsFirst)
        {
            Scrollbar.value = GlobalHelper.backGround.volume;
            m_BYES.onClick.AddListener(delegate () { this.SaveAlter(); });
            m_BNO.onClick.AddListener(delegate () { this.CancelAlter(); });
            Transform m_Parent = GlobalHelper.FindGameObjectWithName(m_back, "Game").transform;
            count = m_Parent.childCount;
            m_toggle = new Toggle[count];
            for(int i = 0; i < count; i++)
            {
                m_toggle[i] = m_Parent.GetChild(i).GetComponent<Toggle>();
            }
            IsFirst = false;
        }
    }
    void SaveAlter()
    {
        m_BICO.interactable = true;
        m_bAlter = false;
        for(int i = 0; i < count; i++)
        {
            if (m_toggle[i].isOn)
            {
                PlayerPrefs.SetInt("IsShow" + m_toggle[i].name + "Info", 1);
            }
            else
            {
                PlayerPrefs.SetInt("IsShow" + m_toggle[i].name + "Info", 0);
            }
        }
        m_back.SetActive(false);
    }
    void CancelAlter()
    {
        m_BICO.interactable = true;
        m_bAlter = false;
        m_back.SetActive(false);
    }
}
